using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO; 

namespace Administration_Toolkit_v2
{
    public partial class ExportResxFiles : Form
    {
        public ExportResxFiles()
        {
            InitializeComponent();
            
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cmbApplications_SelectedIndexChanged(object sender, EventArgs e)
        {
            string strApplicationName = cmbApplications.Text;
            cmbFiles.DataSource = Utilities.GetApplicationFileList(strApplicationName);
            cmbFiles.ValueMember = "DataSourceID";
            cmbFiles.DisplayMember = "DataSourceName";
            cmbCultures.DataSource = Utilities.GetApplicationCultureList(cmbApplications.Text);
            cmbCultures.ValueMember = "CultureID";
            cmbCultures.DisplayMember = "CultureName"; 

        }

        private void ExportResxFiles_Load(object sender, EventArgs e)
        {
            cmbApplications.DataSource = Utilities.GetApplicationList();
            cmbApplications.ValueMember = "ApplicationID";
            cmbApplications.DisplayMember = "ApplicationName";
            cmbFiles.DataSource = Utilities.GetApplicationFileList(cmbApplications.Text);
            cmbFiles.ValueMember = "DataSourceID";
            cmbFiles.DisplayMember = "DataSourceName";
            cmbCultures.DataSource = Utilities.GetApplicationCultureList(cmbApplications.Text);
            cmbCultures.ValueMember = "CultureID";
            cmbCultures.DisplayMember = "CultureName"; 


        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtDirectory.Text == null || txtDirectory.Text == String.Empty)
                {
                    txtStatus.Text = "Please Select a Directory";
                }
                else
                {
                    string strApplicationName;
                    string strCultureName;
                    string strFileName;
                    string strDirectoryName;
                    DirectoryInfo di;
                   
                    if (cmbFiles.Text.Trim() == "All")
                    {
                        foreach (DataRowView tmpFile in cmbFiles.Items)
                        {
                            if (tmpFile[1].ToString().Trim() != "All")
                            {
                                if (cmbCultures.Text.Trim() == "All")
                                {
                                    foreach (DataRowView tmpCulture in cmbCultures.Items)
                                    {
                                        if (tmpCulture[1].ToString().Trim() != "All")
                                        {
                                            strApplicationName = cmbApplications.Text;
                                            strCultureName = tmpCulture[1].ToString();
                                            strDirectoryName = txtDirectory.Text;
                                            strFileName = tmpFile[1].ToString();
                                            di = new DirectoryInfo(strDirectoryName + strFileName.Substring(0, strFileName.LastIndexOf(@"\")));
                                            di.Create(); 
                                            Utilities.ExportResxFile(strApplicationName, strCultureName, strFileName,strDirectoryName);
                                        }
                                    }
                                }
                                else
                                {
                                    strApplicationName = cmbApplications.Text;
                                    strCultureName = cmbCultures.Text;
                                    strFileName = txtDirectory.Text +  tmpFile[1].ToString();
                                    strDirectoryName = txtDirectory.Text;
                                    strFileName = tmpFile[1].ToString();
                                    di = new DirectoryInfo(strDirectoryName + strFileName.Substring(0, strFileName.LastIndexOf(@"\")));
                                    di.Create();
                                    Utilities.ExportResxFile(strApplicationName, strCultureName, strFileName, strDirectoryName);
                                }
                            }
                        }
                    }
                    else if (cmbCultures.Text.Trim() == "All")
                    {
                        foreach (DataRowView tmpCulture in cmbCultures.Items)
                        {
                            if (tmpCulture[1].ToString().Trim() != "All")
                            {
                                strApplicationName = cmbApplications.Text;
                                strCultureName = tmpCulture[1].ToString();
                                strFileName = cmbFiles.Text;
                                strDirectoryName = txtDirectory.Text;
                                di = new DirectoryInfo(strDirectoryName + strFileName.Substring(0, strFileName.LastIndexOf(@"\")));
                                di.Create();
                                Utilities.ExportResxFile(strApplicationName, strCultureName, strFileName,strDirectoryName);
                            }
                        }

                    }
                    else
                    {


                        strApplicationName = cmbApplications.Text;
                        strCultureName = cmbCultures.Text;
                        strFileName = cmbFiles.Text;
                        strDirectoryName = txtDirectory.Text;
                        di = new DirectoryInfo(strDirectoryName + strFileName.Substring(0, strFileName.LastIndexOf(@"\")));
                        di.Create();
                        Utilities.ExportResxFile(strApplicationName, strCultureName, strFileName,strDirectoryName);
                    }
                    txtStatus.Text = "Files Exported";
                }
            }
            catch(Exception ex)
            {
                txtStatus.Text = ex.Message;   
            }

        }

        private void btnSelectDirectory_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.ShowDialog();
            txtDirectory.Text = folderBrowserDialog1.SelectedPath;   
        }

        private void lblDirectory_Click(object sender, EventArgs e)
        {

        }

        

       
    }
}